using System.Xml;
using System.Collections.Generic;
using Guadagno.Utilities.Configuration;

namespace ApplicationPlugins
{
    /// <summary>
    /// The configuration file for the File Copy plugin
    /// </summary>
    public class FileCopyConfig : ConfigurationBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FileCopyConfig"/> class.
        /// </summary>
        public FileCopyConfig() { }
        /// <summary>
        /// Initializes a new instance of the <see cref="FileCopyConfig"/> class.
        /// </summary>
        /// <param name="stage">The stage.</param>
        public FileCopyConfig(string stage) : base(stage) { }
        /// <summary>
        /// Initializes a new instance of the <see cref="FileCopyConfig"/> class.
        /// </summary>
        /// <param name="stage">The stage.</param>
        /// <param name="filename">The filename.</param>
        public FileCopyConfig(string stage, string filename) : base(stage, filename) { }

        #region Public Properties From XML

        /// <summary>
        /// Gets the schedule.
        /// </summary>
        /// <value>The schedule.</value>
        public string Schedule { get { return this["Schedule"]; } }
        /// <summary>
        /// Gets the log4 net config.
        /// </summary>
        /// <value>The log4 net config.</value>
        public string Log4NetConfig { get { return this["log4net"]; } }

        /// <summary>
        /// Gets the directories.
        /// </summary>
        /// <value>The directories.</value>
        public ConfigDirectory[] Directories
        {
            get
            {
                List<ConfigDirectory> dirs = new List<ConfigDirectory>();

                if (!string.IsNullOrEmpty(this["Directories"]))
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(this["Directories"]);
                    XmlNode directories = xml.SelectSingleNode("Directories");

                    foreach (XmlNode dir in directories.ChildNodes)
                    {
                        if (dir.NodeType != XmlNodeType.Element) continue;

                        ConfigDirectory cDir = new ConfigDirectory
                                                   {
                                                       FromDir = getAttribValue(dir, "FromDir", ""),
                                                       ToDir = getAttribValue(dir, "ToDir", ""),
                                                       FileMask = getAttribValue(dir, "FileMask", "")
                                                   };
                        dirs.Add(cDir);
                    }
                }
                return dirs.ToArray();
            }
        }

        #endregion Public Properties From XML

        #region ConfigDirectory Struct

        ///<summary>
        /// A helper structure for the file copy
        ///</summary>
        public struct ConfigDirectory
        {
            ///<summary>
            /// The directory to copy from
            ///</summary>
            public string FromDir { get; set;}
            /// <summary>
            /// The directory to copy to
            /// </summary>
            public string ToDir { get; set;}
            /// <summary>
            /// The file mask to copy
            /// </summary>
            public string FileMask { get; set;}
        }
        #endregion ConfigDirectory Struct
    }
}
